//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.server;

import java.util.ArrayList;
import java.util.List;

import tv.dyndns.kishibe.client.packet.PacketPlayerAction;
import tv.dyndns.kishibe.client.packet.PacketResult;

public class PlayerStatus {
	private String name;
	private String answer;
	private boolean answered = false;
	private int score;
	private int playerId;
	private int playerListId;
	private int sessionId;
	private int timeRemain;
	private boolean requestStartingGame = false;
	private int rank;
	private int rating;
	private boolean human;
	private int skipCount;
	private String greeting;
	private String imageFileName;
	private List<PacketPlayerAction> list = new ArrayList<PacketPlayerAction>();
	private int tempRanking;
	private int classLevel;
	private int userCode;

	public PlayerStatus(String name, int playerId, int playerListId, int sessionId, boolean isHuman, String greeting, String imageFileName, int classLevel, int rating, int userCode) {
		this.name = name;
		this.playerId = playerId;
		this.playerListId = playerListId;
		this.sessionId = sessionId;
		this.skipCount = 3;
		this.human = isHuman;
		this.greeting = greeting;
		this.imageFileName = imageFileName;
		this.classLevel = classLevel;
		this.rating = rating;
		this.userCode = userCode;
	}

	public void clear() {
		answer = null;
		answered = false;
	}

	public void setAnswer(String answer, int timeRemain) {
		if (answered) {
			return;
		}

		this.answer = answer;
		this.answered = true;
		this.timeRemain = timeRemain;
	}

	public boolean isAnswered() {
		return answered;
	}

	public String getAnswer() {
		return answer;
	}

	public boolean isCorrect(String answer[]) {
		if (this.answer == null) {
			return false;
		}
		for (String tempAnswer : answer) {
			if (this.answer.equals(tempAnswer)) {
				return true;
			}
		}
		return false;
	}

	public int getTimeRemain() {
		return timeRemain;
	}

	public void pushPlayerAction(PacketPlayerAction playerAction) {
		if (list == null) {
			list = new ArrayList<PacketPlayerAction>();
		}
		list.add(playerAction);
	}

	public List<PacketPlayerAction> popPlayerAction() {
		List<PacketPlayerAction> playerAction = list;
		list = null;
		return playerAction;
	}

	public void setRequestStartingGame() {
		requestStartingGame = true;
	}

	public boolean isRequestStartingGame() {
		return requestStartingGame;
	}

	public void incSkipCount() {
		++skipCount;
	}

	public void clearSkipCount() {
		skipCount = 0;
	}

	public boolean isDrop() {
		return skipCount >= 1;
	}

	public int getSessionId() {
		return sessionId;
	}

	public int getPlayerId() {
		return playerId;
	}

	public int getPlayerListId() {
		return playerListId;
	}

	public String getName() {
		return name;
	}

	public void dropPlayer() {
		human = false;
		if (!name.startsWith("(")) {
			int offset = name.indexOf(' ');
			name = "(堕) " + name.substring(offset + 1);
		}
	}

	public boolean isHuman() {
		return human;
	}

	public void addScore(int score) {
		this.score += score;
	}

	public int getScore() {
		return score;
	}

	public void setRating(int rating) {
		this.rating = rating;
	}

	public int getRating() {
		return rating;
	}

	public void setRank(int rank) {
		this.rank = rank;
	}

	public PacketResult toResult() {
		PacketResult result = new PacketResult();
		result.name = this.name;
		result.playerId = this.playerId;
		result.playerListId = this.playerListId;
		result.rank = this.rank;
		result.score = this.score;
		result.sessionId = this.sessionId;
		result.ratingDelta = this.rating;
		result.imageFileName = this.imageFileName;
		return result;
	}

	public String getGreeting() {
		return greeting;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setTempRanking(int tempRanking) {
		this.tempRanking = tempRanking;
	}

	public int getTempRanking() {
		return tempRanking;
	}

	public int getClassLevel() {
		return classLevel;
	}

	public int getUserCode() {
		return userCode;
	}
}
